package org.jeecg.modules.his.service.impl;

import org.jeecg.modules.his.entity.HisArrears;
import org.jeecg.modules.his.entity.HisSubArrear;
import org.jeecg.modules.his.mapper.HisSubArrearMapper;
import org.jeecg.modules.his.mapper.HisArrearsMapper;
import org.jeecg.modules.his.service.IHisArrearsService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 欠费历史
 * @Author: jeecg-boot
 * @Date:   2022-02-22
 * @Version: V1.0
 */
@Service
public class HisArrearsServiceImpl extends ServiceImpl<HisArrearsMapper, HisArrears> implements IHisArrearsService {

	@Autowired
	private HisArrearsMapper hisArrearsMapper;
	@Autowired
	private HisSubArrearMapper hisSubArrearMapper;
	
	@Override
	@Transactional
	public void saveMain(HisArrears hisArrears, List<HisSubArrear> hisSubArrearList) {
		hisArrearsMapper.insert(hisArrears);
		if(hisSubArrearList!=null && hisSubArrearList.size()>0) {
			for(HisSubArrear entity:hisSubArrearList) {
				//外键设置
				entity.setArrearId(hisArrears.getId());
				hisSubArrearMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(HisArrears hisArrears,List<HisSubArrear> hisSubArrearList) {
		hisArrearsMapper.updateById(hisArrears);
		
		//1.先删除子表数据
		hisSubArrearMapper.deleteByMainId(hisArrears.getId());
		
		//2.子表数据重新插入
		if(hisSubArrearList!=null && hisSubArrearList.size()>0) {
			for(HisSubArrear entity:hisSubArrearList) {
				//外键设置
				entity.setArrearId(hisArrears.getId());
				hisSubArrearMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		hisSubArrearMapper.deleteByMainId(id);
		hisArrearsMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			hisSubArrearMapper.deleteByMainId(id.toString());
			hisArrearsMapper.deleteById(id);
		}
	}
	
}
